layui.use(["table","layer","jquery"],function(){
    var table = layui.table;
    var $ = layui.jquery;
    var layer = layui.layer;
    table.render({
        elem:"#userList",
        url:"/manager/getUserList",
        page:true,
        cols:[[
            {field: 'userId', title: 'ID', width:'5%', sort: true, fixed: 'left'}
                ,{field: 'username', title: '用户名', width:'15%', sort: true}
                ,{field: 'sex', title: '性别', width:'10%'}
                ,{field: 'telphone', title: '电话', width:'15%'}
                ,{field: 'email', title: '邮箱', width: '20%'}
                ,{field: 'roleId', title: '角色', width: '10%', templet:"#roleShow"}
                ,{fixed: 'right', title:"操作",width:'24.5%', align:'center', toolbar: '#userListToolBar'}
        ]],
        toolbar:true,
        skin:"line",
        toolbar:"#tableTopBar"
    });
    table.on('tool(test)',function(obj){
        var data = obj.data;
        var layEvent = obj.event;
        var tr = obj.tr;
        if(layEvent === 'editPwd'){
            layer.prompt({title: '请输入重置的密码', formType: 2}, function(text, index){
                $.post({
                    method:"post",
                    url:"/user/pwdChange/"+data.userId+"/"+text,
                    data:data.userId,
                    success:function(data){
                        layer.msg(data);
                    },
                    error:function(data){
                        layer.msg(data);
                    }
                })
                layer.close(index);
                layer.msg('密码已经重置为:'+text);
            });

        };
        if(layEvent === 'edit'){
            layer.open({
                type:2,
                content:"/user/userManagerEdit/"+data.userId,
                area:['800px','600px']
            });
        };
        if(layEvent === 'updateAuth'){
            layer.open({
                type:2,
                content:"/manager/updateAuth/"+data.userId,
                area:['500px','300px'],
                end:function(){
                    location.reload();
                }
            });
        };
        if(layEvent === 'delete'){
            layer.confirm('确定要删除用户 "'+data.username+'" 吗?',{
                btn:['确认','取消']
            },
            function(){
                $.post({
                    method:"post",
                    url:"/user/userDelete/"+data.userId,
                    data:data.userId,
                    success:function(data){
                        layer.msg(data);
                    },
                    error:function(data){
                        layer.msg(data);
                    }
                })
                location.reload();
            },function(){
                layer.msg("已取消!");
            });
        };
    });
    table.on('toolbar(test)',function(obj){
        var checkStatus = table.checkStatus(obj.config.id);
        switch(obj.event){
            case 'search':
                var keyword = $("#userSearchInput").val();
                if(keyword!=""){
                    table.reload("userList",{
                        url:"/manager/userSearch/"+keyword
                    });
                }else{
                    layer.msg("不能检索空值");
                }
            break;
        };
    });

})